class MicropostsController < ApplicationController
  before_action :set_micropost, only: [:show, :edit, :update, :destroy]

  def index
    @microposts = Micropost.all
  end

  def show
  end

  def new  
    if logged_in? 
      @micropost = Micropost.new
    else
      redirect_to login_path
    end
  end


  def edit
    @micropost = Micropost.find(params[:id])

  end

  def create    
    @micropost = Micropost.new(micropost_params)
    @micropost.user_id = current_user.id

    if @micropost.save
      redirect_to current_user
    else
      render :new
    end
  end


  def update
    if @micropost.update(micropost_params)
      redirect_to current_user
    else
      render :edit
    end
  end

  def destroy
    @micropost.destroy
    redirect_to current_user
  end

  private
    def set_micropost
      @micropost = Micropost.find(params[:id])
    end

    def micropost_params
      params.require(:micropost).permit(:task, :date, :cost, :user_id, :category_id, :subcategory_id, :jobnumber_id)
    end
end
